package shun.demo.process.monitor;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import shun.demo.R;

import java.util.Observable;
import java.util.Observer;

/**
 * Created by chen on 14-4-1.
 */
public class ProcessMonitorActivity extends Activity implements View.OnClickListener {

    private ActivityMonitor mActivityMonitor;
    private NotificationManager mNotificationManager;
    private static final int NOTIFICATION_ID = 1;
    private ComponentName mLastComponentName;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.process_monitor);
        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mActivityMonitor = new ActivityMonitor(this);
        mActivityMonitor.addObserver(mObserver);
    }

    private Observer mObserver = new Observer() {
        @Override
        public void update(Observable observable, Object data) {
            ComponentName componentName = (ComponentName) data;
            if (!equals(componentName, mLastComponentName)) {
                mLastComponentName = componentName;
                if (componentName == null) {
                    mLastComponentName = null;
                    mNotificationManager.cancel(NOTIFICATION_ID);
                    NotificationCompat.Builder builder = new NotificationCompat.Builder(ProcessMonitorActivity.this);
                    builder.setTicker("No Activity");
                    builder.setContentTitle("No Activity");
                    Notification notification = builder.build();
                    mNotificationManager.notify(NOTIFICATION_ID, notification);
                } else {
                    mNotificationManager.cancel(NOTIFICATION_ID);
                    NotificationCompat.Builder builder = new NotificationCompat.Builder(ProcessMonitorActivity.this);
                    builder.setTicker("Activity Changed");
                    builder.setContentTitle(mLastComponentName.getPackageName());
                    builder.setContentInfo(mLastComponentName.getClassName());
                    builder.setSmallIcon(R.drawable.ic_launcher);
                    Notification notification = builder.build();
                    mNotificationManager.notify(NOTIFICATION_ID, notification);
                }
            }
        }

        private boolean equals(ComponentName a, ComponentName b) {
            if (a == null) {
                if (b == null) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return a.equals(b);
            }
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start:
                mActivityMonitor.start();
                break;
            case R.id.stop:
                mNotificationManager.cancel(NOTIFICATION_ID);
                mActivityMonitor.stop();
                break;
        }
    }
}